package com.jt.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jt.mapper.RightsMapper;
import com.jt.mapper.RoleRightsMapper;
import com.jt.pojo.Rights;
import com.jt.pojo.RoleRights;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class RightsServiceImpl implements RightsService{

    @Autowired
    private RightsMapper rightsMapper;

    @Autowired
    private RoleRightsMapper roleRightsMapper;

    @Override
    public List<Rights> findAll() {
        return rightsMapper.selectList(null);
    }

    @Override
    @Transactional
    public void updateRights(Rights rights) {
        rightsMapper.updateById(rights);
    }

    @Override
    public List<Rights> getRightsListByIds(List<Integer> rightsIds) {
        //1.获取所有的一级菜单
        List<Rights> oneRightsList = findRightsListByLevelAndIds(0,1,rightsIds);

        //2.封装二级查询菜单
        for (Rights oneRights : oneRightsList){
            List<Rights> twoRightsList = findRightsListByLevelAndIds(oneRights.getId(),2,rightsIds);

            //3.封装3级菜单
            for(Rights twoRights :twoRightsList){
                List<Rights> threeRightsList = findRightsListByLevelAndIds(twoRights.getId(),3,rightsIds);
                twoRights.setChildren(threeRightsList);
            }
            oneRights.setChildren(twoRightsList);
        }
        return oneRightsList;
    }

    public List<Rights> findRightsListByLevelAndIds(Integer parentId,Integer level,List<Integer> ids){
        QueryWrapper<Rights> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("level", level)
                .eq("parent_id", parentId)
                .in("id",ids);
        return rightsMapper.selectList(queryWrapper);
    }


    /**指定角色(roleID)的所有leve为三级点的权限的id集合
     * 获取
     * @param roleId
     * @return
     */
    @Override
    public List<Integer> getThreeRightsIds(Integer roleId) {
        return rightsMapper.getThreeRightsIds(roleId);
    }

    /**
     * —级权限
     * children----->二级权限
     *                children------->三级权限
     */
    @Override
    public List<Rights> getRightsListAll() {
        Map<Integer,List<Rights>> map=getRightsMap();
        List<Rights> oneList=map.get(0);
        for (Rights rights:oneList){
            List<Rights> twoList=map.get(rights.getId());
            rights.setChildren(twoList);
        }
        for (Rights rights:oneList)
        {
            if(rights.getChildren()!=null)
            {
                for(Rights rights2:rights.getChildren())
                {
                    List<Rights> threeList=map.get(rights2.getId());
                    rights2.setChildren(threeList);
                }
            }
        }

        return oneList;
    }

    public  List<Rights> findChildrenList(Integer rightsId)
    {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("parent_id",rightsId);
        return  rightsMapper.selectList(queryWrapper);
    }

    @Override
    public List<Integer> getRightsChildrenList(Integer rightsId) {
        List<Integer> rightsIdList=new ArrayList<>();
        List<Rights> childrenList=findChildrenList(rightsId);
        for(Rights rights:childrenList)
        {
            rightsIdList.add(rights.getId());
        }
        return rightsIdList;
    }

    private Map<Integer, List<Rights>> getRightsMap() {
        List<Rights> rightsList = rightsMapper.selectList(null);
        Map<Integer,List<Rights>> map= new HashMap<>();
        for (Rights rights:rightsList)
        {
            Integer parentId=rights.getParentId();
            if(map.containsKey(parentId))
                map.get(parentId).add(rights);
            else {
                List<Rights> list=new ArrayList<>();
                list.add(rights);
                map.put(parentId,list);
            }
        }
        return map;
    }


    /**
     * 需求: 查询2级菜单信息.
     * 步骤:
     *      1.查询一级菜单信息 parent_id = 0
     *      2.查询二级菜单信息 parent_id = 一级菜单ID
     *      3.将二级菜单封装到一级菜单中.
     *  查询方式:
     *          1.利用java循环实现2级封装.
     *          2.利用Mybatis的封装的方式实现查询 resultMap 自己练习
     * @return
     */

    @Override
    public List<Rights> getRightsList() {
        QueryWrapper queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("parent_id", 0);
        List<Rights> rightsOneList = rightsMapper.selectList(queryWrapper);
        for (Rights oneRights:rightsOneList) {
            queryWrapper.clear();
            queryWrapper.eq("parent_id", oneRights.getId());
            List<Rights> rightsTwoList = rightsMapper.selectList(queryWrapper);
            oneRights.setChildren(rightsTwoList);
        }
        return rightsOneList;
    }


}
